<?php
/**
 * articles de la page presse
 *
 * Description de la classe
 *
 * @author David
 */
class NewspapersPost extends Object {
    protected $id;
    protected $citation;
    protected $lien_article;
    protected $logo;
    protected $date_article;

    function __construct($id="", $params=array()) {
        if($id != "") {
            $this->id = $id;
            foreach($params as $key => $p) {
                $this->$key = $p;
            }
        }
    }

    function __toString() {
        if(!isset($this->citation)) $this->load();
        return $this->citation;
    }

    public function load() {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM presse_art WHERE id = '".$this->id."'");
        if(sizeof($result) > 0) {
            foreach($result[0] as $key => $p) {
                $this->$key = $p;
            }
            return;
        }
        if(DEBUG) echo("Objet inconnu");
        return false;
    }

    public static function getAllNewspapersPosts($limit_start="", $limit_end=""){
        $db = Database::getInstance();
        $sql = "SELECT * FROM presse_art ORDER BY date_article DESC";
        if(($limit_start==0 || $limit_start!="") && $limit_end!=""){
            $sql.=" LIMIT ".$limit_start.", ".$limit_end;
        }
        $result = $db->query($sql);
        $allNewspapersPosts = array();
        foreach($result as $line){
            $allNewspapersPosts[] = new NewspapersPost($line['id'], $line);
        }
        return $allNewspapersPosts;
    }

    public function getFormatedDate(){
        $tab=explode("-",$this->date_article);
        $jour_et_heure = $tab[2];
        $tabjour = explode(" ",$jour_et_heure);
        $jour = $tabjour[0];
        if($jour[0]==0){
            $jour = $jour[1];
        }
        $mois_en_chiffre = $tab[1];
        $annee = $tab[0];
        $les_mois = array('01'=>'Janvier','02'=>'F&eacute;vrier','03'=>'Mars','04'=>'Avril','05'=>'Mai','06'=>'Juin','07'=>'Juillet','08'=>'Ao&ucirc;t','09'=>'Septembre','10'=>'Octobre','11'=>'Novembre','12'=>'D&eacute;cembre');
        $mois = $les_mois[$mois_en_chiffre];
        $new_date = $jour." ".$mois." ".$annee;
        return $new_date;
    }
}
?>
